package porte;

import java.io.Serializable;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
import java.util.logging.Logger;

import algo.*;
import porte.Porte;
import reso.ServeurDeVoiture;
import serveur_reso.Reso;

public class Porte extends UnicastRemoteObject implements Client, Algorithm {

	private static final long serialVersionUID = -3879530234484702428L;
	
	private int id;
	
	private List<Client> portes;
	
	private Algorithm algorithm;

	private Reso reso;
	
	private String typeAlgo;
	
	public Porte(Reso reso, String algo) throws RemoteException {
		super();
	
		this.reso = reso;
		this.typeAlgo = algo;
		
		System.out.println("================Start porte================");
		//System.out.println("            Le porte est :" + id);
		System.out.println("===========================================");
		
	}

	public int getId() {
		return this.id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	@Override
	public void receiveMessage(int from, int to, Serializable msg)
			throws RemoteException {
		this.algorithm.receiveMessage(from, to, msg);
	}


	@Override
	public void demandeEntree() throws RemoteException {
		algorithm.demandeEntree();
	}


	@Override
	public void demandeSortie() throws RemoteException {
		algorithm.demandeSortie();
	}
	
	public Reso getReso() {
		return this.reso;
	}
	
	public static void main (String [] args) {
		
		try{
			
			if (args.length == 0) {
				System.out.println("Preciser le type d'algo");
			}
			
			System.out.println("\nCreation dune porte\n");
			
			ServeurDeVoiture voiture = (ServeurDeVoiture) Naming.lookup(ServeurDeVoiture.NAME);
			
			Reso reso = (Reso) Naming.lookup(Reso.NAME);
			

			Client porte = new Porte(reso, args[0]);
			System.out.println("Creation porte est ok");
			
			int idporte = reso.declareClient(porte);
			((Porte) porte).setId(idporte);
			
			((Porte) porte).initialise();
			
			System.out.println("id porte est "+idporte);
			
			voiture.addNewPorte(porte);
			
		}
		catch(Exception e) { 
			e.printStackTrace(); 
		}
	}

	@Override
	public void initialise() throws RemoteException {
		
		System.out.println("************************");
		
		if (this.typeAlgo.equals("0")) {
			System.out.println("Algo de Jeton");
			this.algorithm = new AlgoJeton(this);			
		}
		
		if (this.typeAlgo.equals("1")) {
			System.out.println("Algo de Horloge");
			this.algorithm = new AlgoHorloge(this);	
		}
		
		if (this.typeAlgo.equals("2")) {
			System.out.println("Algo de Diffusion ABCAST");
			this.algorithm = new Abcast(this.id, Client.nombreDePortes, Client.nombreDePlaceLibre, reso);	
		}
		
		System.out.println("**********************");
		this.algorithm.initialise();
	}

	/**
	 * @return the portes
	 */
	public List<Client> getPortes() {
		return portes;
	}

	/**
	 * @param portes the portes to set
	 */
	public void setPortes(List<Client> portes) {
		this.portes = portes;
	}
	
	public String toString() {
		return "Je suis porte "+this.id;
	}

}
